package org.footito.android.adapter;

import java.util.Collections;
import java.util.List;

import org.footito.android.R;
import org.footito.android.data.Event;
import org.footito.android.data.Group;
import org.footito.android.data.TeamStatus;
import org.footito.android.data.database.ProviderAdapter;
import org.footito.android.tools.ConvertingTools;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TimelineBaseAdapter extends BaseAdapter {
	public static final int ALL = -1;
	private static String ACTORS_COMS;
	private static String ACTORS_SUBS;
	private static String JOINGROUP;

	private LayoutInflater inflater;
	private List<Event> listEvents;
	// private ArrayList<View> viewList = new ArrayList<View>();
	private Context context;
	private ProviderAdapter providerAdapter;

	public TimelineBaseAdapter(Context context, int teamId) {
		providerAdapter = new ProviderAdapter(context);
		if (teamId == ALL) {
			listEvents = providerAdapter.getEventList();
		} else {
			listEvents = providerAdapter.getEventListFor(teamId);
		}
		Collections.sort(listEvents, new ComparatorEvent<Event>());
		inflater = LayoutInflater.from(context);
		this.context = context;
		JOINGROUP = context.getResources().getString(R.string.joinGroup);
		ACTORS_COMS = context.getResources().getString(R.string.comments);
		ACTORS_SUBS = context.getResources().getString(R.string.subscribers);
	}

	@Override
	public int getCount() {
		return listEvents.size();
	}

	@Override
	public Event getItem(int location) {
		return listEvents.get(location);
	}

	@Override
	public long getItemId(int location) {
		return location;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// if (position < viewList.size())
		// convertView = viewList.get(position);
		Event currentEvent = listEvents.get(position);

		MyTag holder;
		if (convertView == null) {
			holder = new MyTag();
			convertView = createLine(currentEvent, holder);
		} else {
			holder = (MyTag) convertView.getTag();
		}

		if (currentEvent instanceof Group) {
			holder.layoutgroup.setVisibility(View.VISIBLE);
			holder.layoutstatus.setVisibility(View.GONE);

			holder.group.setText(JOINGROUP + " : \"" + currentEvent.getContent() + "\"");
			holder.actorsnum.setText(Integer.toString(((Group) currentEvent).getSubscribers()
					.size()));
			holder.actors.setText(ACTORS_SUBS);
			holder.teamName.setText(currentEvent.getTeam().getName());
		} else if (currentEvent instanceof TeamStatus) {
			holder.layoutgroup.setVisibility(View.GONE);
			holder.layoutstatus.setVisibility(View.VISIBLE);

			holder.teamStatus.setText(Html.fromHtml("<b>" + currentEvent.getTeam().getName()
					+ "</b> : " + currentEvent.getContent()));
			holder.actorsnum.setText(Integer.toString(((TeamStatus) currentEvent).getComments()
					.size()));
			holder.actors.setText(ACTORS_COMS);
		}

		holder.timestamp.setText(ConvertingTools.getFormattedDate(currentEvent.getTimestamp()));

		Bitmap bitmap = currentEvent.getTeam().getPicture(context);
		// if (holder.flag.getTag() == null) {
		// if (bitmap != null) {
		holder.flag.setImageBitmap(bitmap);
		holder.flag.setTag("ok");
		// }
		// }

		return convertView;
	}

	public void refreshData() {
		listEvents = providerAdapter.getEventList();
	}

	private boolean coherent(Event currentEvent, View currentView) {
		MyTag holder = (MyTag) currentView.getTag();
		if (currentEvent instanceof TeamStatus) {
			if (holder.teamStatus != null) {
				return true;
			} else {
				return false;
			}
		} else if (currentEvent instanceof Group) {
			if (holder.group != null) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}

	private View createLine(Event currentEvent, MyTag holder) {
		View result = null;
		// if (currentEvent instanceof Group) {
		// result = inflater.inflate(R.layout.row_timeline_group, null);
		// // viewList.add(result);
		// holder.group = (TextView) result.findViewById(R.id.TV_Group);
		// holder.teamName = (TextView) result.findViewById(R.id.TV_Team_Name);
		// holder.subs = (TextView) result.findViewById(R.id.tv_subs);
		// } else if (currentEvent instanceof TeamStatus) {
		// result = inflater.inflate(R.layout.row_timeline_status, null);
		// // viewList.add(result);
		// holder.teamStatus = (TextView)
		// result.findViewById(R.id.TV_Team_Status);
		// holder.comments = (TextView) result.findViewById(R.id.tv_coms);
		// }

		result = inflater.inflate(R.layout.row_timeline, null);
		holder.group = (TextView) result.findViewById(R.id.TV_Group);
		holder.teamName = (TextView) result.findViewById(R.id.TV_Team_Name);
		holder.actors = (TextView) result.findViewById(R.id.tv_actors);
		holder.actorsnum = (TextView) result.findViewById(R.id.tv_actorsnum);
		holder.teamStatus = (TextView) result.findViewById(R.id.TV_Team_Status);
		holder.timestamp = (TextView) result.findViewById(R.id.TV_date);
		holder.flag = (ImageView) result.findViewById(R.id.IV_Flag);
		holder.layoutstatus = result.findViewById(R.id.layoutstatus);
		holder.layoutgroup = result.findViewById(R.id.layoutgroup);
		result.setTag(holder);
		return result;
	}

	private class MyTag {
		TextView actorsnum;
		ImageView flag;
		TextView teamStatus;
		TextView group;
		TextView teamName;
		TextView timestamp;
		TextView actors;
		View layoutgroup;
		View layoutstatus;
	}

}
